package com.darren.euc.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "euc_orders")
public class Order implements Serializable {

	private static final long serialVersionUID = 7225410464606595171L;


	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	/**
	 * 订单类型：工位订单，会议室订单
	 */
	@Column(name = "order_type")
	private String orderType;

	/**
	 * 订单名称
	 */
	@Column(name = "order_name")
	private String orderName;
	
	/**
	 * 订单编号
	 */
	@Column(name = "order_number")
	private String orderNumber;
	
	/**
	 * 订单描述
	 */
	@Column(name = "description")
	private String description;
	
	/**
	 * 意向省province id
	 */
	@Column(name = "province_id")
	private String provinceId;
	
	/**
	 * 意向城市 city id
	 */
	@Column(name = "city_id")
	private String cityId;
	
	/**
	 * 意向空间id
	 */
	@Column(name = "space_id")
	private Integer spaceId;
	
	/*
	 * 合同
	 */
	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
	@OrderBy("createTime ASC")
	private List<Contract> contractList = new ArrayList<Contract>();
	
	@Transient
	private Integer roomCount = 0;
	
	/**
	 *  合同数
	 */
	@Transient
	private Integer contractCount = 0;
	
	/**
	 * 订单总金额
	 */
	@Transient
	private Double orderAmount = 0D;
	
	/**
	 * 未支付金额
	 */
	@Transient
	private Double noPayAmount = 0D;

	/**
	 * 实付款金客
	 */
	@Transient
	private Double PayAmount = 0D;
	
	/**
	 * 实收押金
	 */
	@Transient
	private Double depositAmount = 0D;
	
	/**
	 * 客户名
	 */
	@Transient
	private String  clientName ="";
	
	/**
	 * 关联客户ID
	 */
	@ManyToOne  
    @JoinColumn(name = "client_id")  
	private Client client;
	
	@Transient
	private Integer clientId;	
	
	@Transient
	private String enterTime;
	
	@Transient
	private String leaveTime;
	
	/**
	 * 起始日期
	 */
	@Transient
	private String startTime;
	
	/**
	 * 结束日期
	 */
	@Transient
	private String endTime;
	
	/**
	 * 销售员
	 */
	@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "sales_person_id", referencedColumnName = "user_id")
	private User salesPerson;
	
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "sales_time",nullable=true,columnDefinition="datetime")
	private Date salesTime;
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "create_time",nullable=true,columnDefinition="datetime")
	private Date createTime;

	@Column(name = "create_user_id")
	private Integer createUserId;
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "last_modify_time",nullable=true,columnDefinition="datetime")
	private Date lastModifyTime;
	
	@Column(name = "last_modify_user_id")
	private Integer lastModifyUserId;
	
	
	@Column(name = "enable")
	private boolean enable = true;
	@Transient
	private String roomNames;
	
	@Transient
	private int orderByNumber;
	
	public int getOrderByNumber() {
		return orderByNumber;
	}

	public void setOrderByNumber(int orderByNumber) {
		this.orderByNumber = orderByNumber;
	}

	public String getRoomNames() {
		return roomNames;
	}

	public void setRoomNames(String roomNames) {
		this.roomNames = roomNames;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}	
	
	

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(String provinceId) {
		this.provinceId = provinceId;
	}

	public String getCityId() {
		return cityId;
	}

	public void setCityId(String cityId) {
		this.cityId = cityId;
	}

	public Integer getSpaceId() {
		return spaceId;
	}

	public void setSpaceId(Integer spaceId) {
		this.spaceId = spaceId;
	}

	public Client getClient() {
		return null;//client;
	}

	public void setClient(Client client) {
		this.client = client;
	}
	
	

	public String getClientName() {
		clientName = "";
		if(client != null){			
			if("个人".equals(client.getClientType())){
				clientName = client.getMemberName();
			}else if("企业".equals(client.getClientType())){
				clientName = client.getCompanyName();
			}
		}		
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Integer getClientId() {
		return (null == client?null:client.getId());//clientId;
	}

	public void setClientId(Integer clientId) {
		this.clientId = clientId;
	}

	public User getSalesPerson() {
		return salesPerson;
	}

	public void setSalesPerson(User salesPerson) {
		this.salesPerson = salesPerson;
	}

	public Date getSalesTime() {
		return salesTime;
	}

	public void setSalesTime(Date salesTime) {
		this.salesTime = salesTime;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Integer getCreateUserId() {
		return createUserId;
	}

	public void setCreateUserId(Integer createUserId) {
		this.createUserId = createUserId;
	}

	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	public Integer getLastModifyUserId() {
		return lastModifyUserId;
	}

	public void setLastModifyUserId(Integer lastModifyUserId) {
		this.lastModifyUserId = lastModifyUserId;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public List<Contract> getContractList() {
		return contractList;
	}

	public void setContractList(List<Contract> contractList) {
		this.contractList = contractList;
	}
	
	

	public String getStartTime() {
		Date date = null;
		for(Contract contract:contractList){
			if(contract.getContractStartTime() != null){				
				if(date == null || date.getTime() > contract.getContractStartTime().getTime()){
					date = contract.getContractStartTime();
				}
			}
			
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		startTime = date == null?"":format.format(date);
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		Date date = null;
		for(Contract contract:contractList){
			if(contract.getContractEndTime() != null){				
				if(date == null || date.getTime() < contract.getContractEndTime().getTime()){
					date = contract.getContractEndTime();
				}
			}
			
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		endTime = date == null?"":format.format(date);
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public Integer getRoomCount() {
		roomCount = 0;
		for(Contract contract:contractList){			
			roomCount += contract.getContractProductList() == null?0:contract.getContractProductList().size();
		}		
		return roomCount;
	}

	public void setRoomCount(Integer roomCount) {
		this.roomCount = roomCount;
	}

	public Integer getContractCount() {
		contractCount = contractList == null?0:contractList.size();
		return contractCount;
	}

	public void setContractCount(Integer contractCount) {
		this.contractCount = contractCount;
	}

	public Double getOrderAmount() {
		orderAmount = 0D;
		for(Contract contract:contractList){			
			orderAmount += contract.getContractAmount() == null?0D:contract.getContractAmount();
		}
		return (double) (Math.round(orderAmount*100)/100.00);
	}

	public void setOrderAmount(Double orderAmount) {
		this.orderAmount = orderAmount;
	}





	public Double getNoPayAmount() {
		noPayAmount = 0D;
		for(Contract contract:contractList){
			for(ContractHirePurchase contractHirePurchase:contract.getContractHirePurchaseList()){
				if(contractHirePurchase.getHirePurchaseNumber() != 0){
					noPayAmount += (contractHirePurchase.getHirePurchaseAmount() - contractHirePurchase.getRealPaymentAmount());
				}				
			}
		}
		return (double) (Math.round(noPayAmount*100)/100.00);
	}

	public void setNoPayAmount(Double noPayAmount) {
		this.noPayAmount = noPayAmount;
	}	

	public Double getPayAmount() {
		PayAmount = 0D;
		for(Contract contract:contractList){
			for(ContractHirePurchase contractHirePurchase:contract.getContractHirePurchaseList()){
				PayAmount += contractHirePurchase.getRealPaymentAmount();
			}
		}
		return PayAmount;
	}

	public void setPayAmount(Double payAmount) {
		PayAmount = payAmount;
	}
	
	

	public Double getDepositAmount() {
		depositAmount = 0D;
		for(Contract contract:contractList){
			for(ContractHirePurchase contractHirePurchase: contract.getContractHirePurchaseList()){
				if(contractHirePurchase.getHirePurchaseNumber() == 0){
					depositAmount += contractHirePurchase.getRealPaymentAmount();
				}
			}
		}
		return (double) (Math.round(depositAmount*100)/100);
	}

	public void setDepositAmount(Double depositAmount) {
		this.depositAmount = depositAmount;
	}

	public String getEnterTime() {	
		Date enter = null;
		SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
		for(Contract contract:contractList){
			if(enter == null){
				enter = contract.getContractStartTime();
			}else if(contract.getContractStartTime() !=null && enter.getTime() > contract.getContractStartTime().getTime()){
				enter = contract.getContractStartTime();
			}
		}
		enterTime = enter == null?"":FORMATTER.format(enter);
		return enterTime;
	}

	public void setEnterTime(String enterTime) {
		this.enterTime = enterTime;
	}

	public String getLeaveTime() {
		Date leave = null;
		SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
		for(Contract contract:contractList){
			if(leave == null){
				leave = contract.getContractEndTime();
			}else if(contract.getContractEndTime() != null && leave.getTime() < contract.getContractEndTime().getTime()){
				leave = contract.getContractStartTime();
			}
		}
		leaveTime = leave == null?"":FORMATTER.format(leave);
		return leaveTime;
	}

	public void setLeaveTime(String leaveTime) {
		this.leaveTime = leaveTime;
	}
	
	
	
	
}
